<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .searchCon {
            width: 800px;
            height: 45px;
            margin: 0 auto;
            display: block;
            text-indent: 10px;
        }
    </style>
</head>

<body>
    <input class="searchCon" type="text">
    <a href="https://zhuanlan.zhihu.com/p/110733457?utm_source=qq&utm_medium=social&utm_oi=1213941421675479040">参考地址</a>
</body>
<script>
    var searchCon = document.getElementsByClassName("searchCon")[0];

    // searchCon.addEventListener("input", function () {
    //     console.log(this.value, "发送请求");
    // })

    /* var timer = null;
    searchCon.addEventListener("input", function () {
        clearTimeout(timer);
        timer = setTimeout(() => {
            console.log(this.value, "发送请求");
        }, 300);
    })
 */

    // searchCon.addEventListener("input", (function () {
    //     var timer = null;
    //     return function () {
    //         clearTimeout(timer);
    //         timer = setTimeout(() => {
    //             console.log(this.value, "发送请求");
    //         }, 300);
    //     }
    // })());

    /* (function () {
        var timer = null;
        return function () {
            clearTimeout(timer);
            timer = setTimeout(() => {
                console.log(this.value, "发送请求");
            }, 300);
        }
    })() */



    function debounce(callback, delay = 300) {
        var timer = null;
        return function () {
            clearTimeout(timer);
            timer = setTimeout(() => {
                callback.call(this);
            }, delay);
        }
    }

    function inputHandler() {
        console.log(this.value, "发送请求");
    }
    searchCon.addEventListener("input", debounce(inputHandler));

</script>

</html>